package com.cskaoyan.service.front;

import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketBrand;
import com.cskaoyan.bean.po.MarketBrandExample;
import com.cskaoyan.bean.vo.front.ShowBrandVO;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther 10-何兴绕
 * @Date 2022年10月14日 13:54
 */
@Service
public class FrontBrandServiceImpl implements FrontBrandService {

    @Autowired
    MarketBrandMapper brandMapper;

    @Override
    public CommonData showBrand(Integer page, Integer limit) {
        MarketBrandExample brandExample = new MarketBrandExample();
        MarketBrandExample.Criteria countCriteria = brandExample.createCriteria();
        countCriteria.andDeletedEqualTo(false);
        long count = brandMapper.countByExample(brandExample);

        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        List<MarketBrand> marketBrands = brandMapper.selectByExample(example);
        List<ShowBrandVO> showBrandVOS = new ArrayList<>();
        for (MarketBrand brand : marketBrands) {
            ShowBrandVO brandVO = new ShowBrandVO();
            brandVO.setId(brand.getId());
            brandVO.setName(brand.getName());
            brandVO.setDesc(brand.getDesc());
            brandVO.setPicUrl(brand.getPicUrl());
            brandVO.setFloorPrice(brand.getFloorPrice());
            showBrandVOS.add(brandVO);
        }
        CommonData<ShowBrandVO> commonData = new CommonData<>();
        commonData.setTotal((int) count);
        commonData.setPages((int) (count / limit + 1));
        commonData.setLimit(limit);
        commonData.setPage(page);
        commonData.setList(showBrandVOS);
        return commonData;
    }

    @Override
    public MarketBrand showBrandDetailById(Integer id) {
        MarketBrand marketBrand = brandMapper.selectByPrimaryKey(id);
        return marketBrand;
    }
}
